package com.example.xm.controller;

import com.example.xm.web.Result;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.UUID;

@RestController

public class UploadController {
    @Value("${upload.src}")
    private String uploadSrc;

    @RequestMapping("/upload")
    public Result upload(MultipartFile file) throws IOException {
        System.out.println("aaaaaaaaaaaaaa"+uploadSrc);
//        原始名
        String originalFilename = file.getOriginalFilename();
        //后缀名
        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
        //fileUrl
        String newFileName = UUID.randomUUID() + substring;
        //size
        long size = file.getSize();
        File file1 = new File(uploadSrc);
        if(!file1.exists()){
            file1.mkdirs();
        }
        File file2 = new File(file1, newFileName);
        file.transferTo(file2);
        System.out.println(newFileName);
        return Result.success("http://localhost:9080/getImg?name="+newFileName);
    }
    @RequestMapping("/uploadVideo")
    public Result uploadVideo(MultipartFile file) throws IOException {
        System.out.println("aaaaaaaaaaaaaa"+uploadSrc);
//        原始名
        String originalFilename = file.getOriginalFilename();
        //后缀名
        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
        //fileUrl
        String newFileName = UUID.randomUUID() + substring;
        //size
        long size = file.getSize();
        File file1 = new File(uploadSrc);
        if(!file1.exists()){
            file1.mkdirs();
        }
        File file2 = new File(file1, newFileName);
        file.transferTo(file2);
        System.out.println(newFileName);
        return Result.success("http://localhost:9080/video?name="+newFileName);
    }
//    @RequestMapping("/upload")
//    public Result upload(MultipartFile file) throws IOException {
//        System.out.println("aaaaaaaaaaaaaa"+uploadSrc);
//        String originalFilename = file.getOriginalFilename();
//        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
//        String newFileName = UUID.randomUUID() + substring;
//        File file1 = new File(uploadSrc);
//        if(!file1.exists()){
//            file1.mkdirs();
//        }
//        File file2 = new File(file1, newFileName);
//        file.transferTo(file2);
//        System.out.println(newFileName);
//        return Result.success(newFileName);
//    }
    @RequestMapping("/getImg")
    public void getImg(String name, HttpServletResponse response) throws IOException {
        FileInputStream fileInputStream = new FileInputStream(uploadSrc+"\\" + name);
        response.setContentType("image/jpeg");
        ServletOutputStream outputStream = response.getOutputStream();
        IOUtils.copy(fileInputStream,outputStream);
        fileInputStream.close();
        outputStream.close();
    }
    @RequestMapping("/dowlon")
    public void dowlon(String name, HttpServletResponse response) throws IOException {
        FileInputStream fileInputStream = new FileInputStream(uploadSrc+"\\" + name);
        response.setContentType("application/octet-stream");
        ServletOutputStream outputStream = response.getOutputStream();
        IOUtils.copy(fileInputStream,outputStream);
        fileInputStream.close();
        outputStream.close();
    }
    @RequestMapping("/video")
    public void video(String name, HttpServletResponse response) throws IOException {
        FileInputStream fileInputStream = new FileInputStream(uploadSrc+"\\" + name);
        ServletOutputStream outputStream = response.getOutputStream();
        response.setContentType("video/mp4");
        long length = fileInputStream.available();
        response.setContentLengthLong(length);
        response.setHeader("Accept-Ranges", "bytes");
        IOUtils.copy(fileInputStream,outputStream);
        fileInputStream.close();
        outputStream.close();
    }

}
